package cn.tianyigps.tainyigpsdatahandler.customer;

import cn.tianyigps.tainyigpsdatahandler.Rest;
import cn.tianyigps.tianyigpskafka.task.bean.Task;
import cn.tianyigps.tianyigpskafka.task.customer.Customer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import java.util.Map;
import java.util.Optional;

/**
 * Created with IDEA
 *
 * @description: 消费者
 * @author: cui.guibin
 * @create: 2019/3/19 20:55
 **/
@Component
public class DataCustomer extends Customer {
    @Autowired
    private Rest rest;
    @Override
    public boolean checkTask(Task task) {

        Map<String, Object> params = task.getParams();
        if (!Optional.ofNullable(params).isPresent())
            return false;
        Object callbackO = params.get("callback");
        if (!Optional.ofNullable(callbackO).isPresent())
            return false;
        String callback = null;
        if (callbackO instanceof String)
            callback = (String) callbackO;
        if (StringUtils.isEmpty(callback))
            return false;
        return true;
    }

    @Override
    public void consume(Task task) {

        Map<String, Object> params = task.getParams();
        params.put("id",task.getId());
        rest.sendData(params);
    }
}
